﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTLiteLogic.Delegates;
using FTLiteLogic.Interface;
using FullThrustLogic.Helper;

namespace FullThrustLogic.ShipComponent.Weapon
{
    public class Beam : iWeapon, IReportableEvent
    {
        private double _baseRange = 12;

        private ReportableEvent _eventReporter;
        public ReportableEvent EventReporter
        {
            get { return _eventReporter; }
        }

        public Beam()
        {
            _eventReporter = new ReportableEvent();
        }

        public int Class
        {
            get; set;
        }

        #region iWeapon Members

        public double Range
        {
            get
            {
                return _baseRange*Class;
            }
            set
            {
            }
        }

        public bool Enabled
        {
            get; set;
        }

        public Arc FireArc
        {
            get; set;
        }

        public void ExcuteAttack(Ship targetedShip, double distance)
        {
            if (Enabled)
            {
                int numberOfDice = 0;

                switch (Class)
                {
                    case 3:
                        if (distance <= 12)
                        {
                            numberOfDice = 3;
                        }
                        else if (distance <= 24)
                        {
                            numberOfDice = 2;
                        }
                        else if (distance <= 36)
                        {
                            numberOfDice = 1;
                        }
                        break;
                    case 2:
                        if (distance <= 12)
                        {
                            numberOfDice = 2;
                        }
                        else if (distance <= 24)
                        {
                            numberOfDice = 1;
                        }
                        break;
                    case 1:
                        if (distance <= 12)
                        {
                            numberOfDice = 1;
                        }
                        break;
                }


                StringBuilder eventDescription = new StringBuilder();

                for (int i = 0; i < numberOfDice; i++)
                {
                    bool isEventSuccess = true;

                    int result = DiceHelper.RollDice();

                    if (result == 4 || result == 5)
                    {
                        eventDescription.AppendLine(@"Hit player " + targetedShip.OwnerPlayer.PlayerType.ToString() + " ship " + targetedShip.Name + " for one damage ");
                        _eventReporter.Report(this, new ReportableEventArgs() { EventDescription = eventDescription.ToString(), IsEventSuccess = isEventSuccess });
                        targetedShip.ApplyDamage(1);
                    }
                    else if (result == 6)
                    {
                        eventDescription.AppendLine(@"Hit player " + targetedShip.OwnerPlayer.PlayerType.ToString() + " ship " + targetedShip.Name + " for two damage ");
                        _eventReporter.Report(this, new ReportableEventArgs() { EventDescription = eventDescription.ToString(), IsEventSuccess = isEventSuccess });
                        targetedShip.ApplyDamage(2);
                    }
                    else
                    {
                        isEventSuccess = false;
                        eventDescription.AppendLine(@"Missed player " + targetedShip.OwnerPlayer.PlayerType.ToString() + " ship " + targetedShip.Name);
                        _eventReporter.Report(this, new ReportableEventArgs() { EventDescription = eventDescription.ToString(), IsEventSuccess = isEventSuccess });
                    }

                   
                }
            }
        }

        public bool InArc(Ship targetingShip, Ship targetedShip)
        {
            double onePointX = targetingShip.PositionX + 1 * Math.Cos(targetingShip.Heading);
            double onePointY = targetingShip.PositionY + 1 * Math.Sin(targetingShip.Heading);

            Double angle = Math.Atan2(targetedShip.PositionY - targetingShip.PositionY,
                targetedShip.PositionX - targetingShip.PositionX) -
                Math.Atan2(targetingShip.PositionY - onePointY, targetingShip.PositionX - onePointX);


            if ((angle >= 330 && angle <= 360) || angle <= 30)
            {
                if (FireArc.Front)
                {
                    return true;
                }
            }
            else if (angle > 30 || angle <= 90)
            {
                if (FireArc.FrontStarboard)
                {
                    return true;
                }
            }
            else if (angle > 90 || angle <= 150)
            {
                if (FireArc.AftStarboard)
                {
                    return true;
                }
            }
            else if (angle > 150 || angle <= 210)
            {
                if (FireArc.Aft)
                {
                    return true;
                }
            }
            else if (angle > 210 || angle <= 270)
            {
                if (FireArc.AftPortside)
                {
                    return true;
                }
            }
            else if (angle > 270 || angle <= 330)
            {
                if (FireArc.AftPortside)
                {
                    return true;
                }
            }

            return false;
        }


        

        #endregion

        
    }
}
